package vip.gadfly.tiktok.config;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 微信接口地址域名部分的自定义设置信息.
 *
 *前端-小程序对接官方文档：https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/api/open-interface/user-information/tt-get-user-info
 * 服务端-小程序官方文档：https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/server-api-introduction
 *
 *
 * @author Gadfly
 * @since 2021-09-15
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TtOpHostConfig {
    /**
     * 旧抖音开放平台：网站应用与移动应用接口
     */
    public static final String TIKTOK_OPEN_API_HOST_URL = "https://open.douyin.com";
    public static final String TOUTIAO_OPEN_API_HOST_URL = "https://open.snssdk.com";
    public static final String XIGUA_OPEN_API_HOST_URL = "https://open-api.ixigua.com";
    public static final String TIKTOK_SILENT_OPEN_API_HOST_URL = "https://aweme.snssdk.com";

    /**
     * 字节第三方平台：头条小程序与支付
     */
    public static final String TIKTOK_PARTNER_OPEN_API_HOST_URL = "https://open.microapp.bytedance.com";

    //抖音小程序
    public static final String TIKTOK_TOUTIAO_MINI_API_HOST_URL = "https://developer.toutiao.com";

    /**
     * 抖音小店
     */
    public static final String TIKTOK_MALL_API_HOST_URL = "https://openapi-fxg.jinritemai.com";

    /**
     * 对应于：https://open.douyin.com
     */
    private String tiktokOpenHost;
    /**
     * 对应于：https://developer.toutiao.com
     */
    private String tiktokMiniHost;

    /**
     * 对应于：https://open.snssdk.com
     */
    private String toutiaoOpenHost;

    /**
     * 对应于：https://open-api.ixigua.com
     */
    private String xiguaOpenHost;

    /**
     * 对应于：https://openapi-fxg.jinritemai.com
     */
    private String toutiaoMallHost;


    public static String buildUrl(TtOpHostConfig hostConfig, String prefix, String path) {
        if (hostConfig == null) {
            return prefix + path;
        }

        if (hostConfig.getTiktokOpenHost() != null && prefix.equals(TIKTOK_OPEN_API_HOST_URL)) {
            return hostConfig.getTiktokOpenHost() + path;
        }

        if (hostConfig.getXiguaOpenHost() != null && prefix.equals(XIGUA_OPEN_API_HOST_URL)) {
            return hostConfig.getXiguaOpenHost() + path;
        }

        if (hostConfig.getToutiaoOpenHost() != null && prefix.equals(TOUTIAO_OPEN_API_HOST_URL)) {
            return hostConfig.getToutiaoOpenHost() + path;
        }

        if (hostConfig.getTiktokMiniHost() != null && prefix.equals(TIKTOK_TOUTIAO_MINI_API_HOST_URL)) {
            return hostConfig.getTiktokMiniHost() + path;
        }

        if (hostConfig.getToutiaoMallHost() != null && prefix.equals(TIKTOK_MALL_API_HOST_URL)) {
            return hostConfig.getToutiaoMallHost() + path;
        }

        return prefix + path;
    }
}
